<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入班级名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业" prop="major">
        <el-select v-model="queryParams.major" placeholder="请选择专业" size="small">
          <el-option
            v-for="dict in majors"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['edu:class:add']">创建班级</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"></el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-card class="box-card" v-for="(item, i) in list" :key="item.id">
      <div slot="header" class="clearfix">
        <span>{{ item.name }}</span>
      </div>
      <el-empty description="暂无班级" v-if="item.classList && item.classList.length <= 0"></el-empty>
      <el-row :gutter="40" class="panel-group" v-if="item.classList && item.classList.length > 0">
        <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col" v-for="(cls, n) in item.classList" :key="n">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>{{ cls.name }}</span>
              <el-button style="float: right; padding: 3px 0" type="text">暂无任课</el-button>
            </div>
            <div class="text item">
              <div class="block" @click="handleMember(cls)" v-hasPermi="['edu:classMember:list']">
                <div><el-badge :value="cls.num" class="item"><i class="el-icon-user-solid"></i></el-badge></div>
                <span class="demonstration">班级成员</span>
              </div>
              <div class="block" @click="handleUpdate(cls)" v-hasPermi="['edu:class:edit']">
                <div><i class="el-icon-s-tools"></i></div>
                <span class="demonstration">编辑班级</span>
              </div>
              <div class="block no-border" @click="handleDelete(cls)" v-hasPermi="['edu:class:remove']">
                <div><i class="el-icon-delete-solid"></i></div>
                <span class="demonstration">删除班级</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-card>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="专业" prop="majorId">
          <el-select v-model.number="form.majorId" placeholder="请选择专业" size="small">
            <el-option
              v-for="dict in majors"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="班别" prop="name">
          <el-input v-model="form.name" placeholder="请输入班别" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClass, getClass, addClass, updateClass, delClass } from "@/api/edu/class";
import { listMajor } from "@/api/edu/major";

export default {
  name: "Class",
  dicts: ['sys_bool_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      majors: [],
      // 是否显示弹出层
      open: false,
      activeName: "public",
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        major: undefined,
        name: undefined
      },
      // 表单参数
      form: {
        id: undefined,
        majorId: undefined,
        name: undefined
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "班别不能为空", trigger: "blur" }
        ],
        majorId: [
          { required: true, message: "请选择专业", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getMajorList();
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listClass(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getMajorList(){
      listMajor({status: 1, page: 1, limit: 100}).then(response => {
        this.majors = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        majorId: undefined,
        name: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleMember(cls){
      this.$router.push({ path: '/people/classMember?classId='+cls.id });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班级";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getClass(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改班级";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateClass(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClass(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delClass(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
    
  }
};
</script>
<style scoped lang="scss">
  .panel-group{
    .card-panel-col{
      margin: 10px 0;
    }
  }
  .box-card{
    margin-bottom: 20px;
  }
  .block {
    text-align: center;
    border-right: 1px solid #eff2f6;
    display: inline-block;
    width: 33%;
    box-sizing: border-box;
    i {
      font-size: 36px;
    }
    .demonstration{
      display: block;
      padding: 10px 0;
    }
  }
  .no-border{
    border-right: 0px;
  }
</style>